package com.wuming.house.service.impl;

import com.wuming.house.core.BusinessException;
import com.wuming.house.core.HouseContant;
import com.wuming.house.core.WumingErrorCode;
import com.wuming.house.dao.SecondhouseMapper;
import com.wuming.house.model.Secondhouse;
import com.wuming.house.service.SecondhouseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("secondhouseService")
public class SecondhouseServiceImpl extends BaseServiceImpl implements SecondhouseService {

    @Autowired
    private SecondhouseMapper secondhouseMapper;

    @Override
    public void saveSecondhouse(Secondhouse house) throws BusinessException {
        secondhouseMapper.insert(house);
    }

    @Override
    public void modifySecondhouse(Secondhouse house) throws BusinessException {
        secondhouseMapper.updateByPrimaryKey(house);
    }

    @Override
    public void removeSecondhouseById(Long id) throws BusinessException {
        secondhouseMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Secondhouse getSecondhouseById(Long id) throws BusinessException {
        return secondhouseMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Secondhouse> getAllSecondhouse(String houseDesc, String area, String price, String layout) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isNotBlank(houseDesc)) {
            params.put("houseDesc", houseDesc);
        }
        if (org.apache.commons.lang.StringUtils.isNotBlank(area) && !HouseContant.ALL_DEFAULT_SELECT.equalsIgnoreCase(area) && !HouseContant.DEFAULT_AREA_DESC.equalsIgnoreCase(area)) {
            params.put("area", area);
        }
        if (org.apache.commons.lang.StringUtils.isNotBlank(price) && !HouseContant.ALL_DEFAULT_SELECT.equalsIgnoreCase(price) && !HouseContant.DEFAULT_TOTAL_PRICE_DESC.equalsIgnoreCase(price)) {
            if (org.apache.commons.lang.StringUtils.startsWith(price, "<")) {
                String endPrice = org.apache.commons.lang.StringUtils.substring(price, 1);
                params.put("endPrice", Integer.parseInt(endPrice));
            } else if (org.apache.commons.lang.StringUtils.startsWith(price, ">")) {
                String startPrice = org.apache.commons.lang.StringUtils.substring(price, 1);
                params.put("startPrice", Integer.parseInt(startPrice));
            } else {
                String[] priceArr = org.apache.commons.lang.StringUtils.split(price, "-");
                params.put("startPrice", priceArr[0]);
                params.put("endPrice", priceArr[1]);
            }
        }
        if (org.apache.commons.lang.StringUtils.isNotBlank(layout) && !HouseContant.ALL_DEFAULT_SELECT.equalsIgnoreCase(layout) && !HouseContant.DEFAULT_LAYOUT_DESC.equalsIgnoreCase(layout)) {
            params.put("layout", layout);
        }
        return secondhouseMapper.selectByParams(params);
    }

    @Override
    public List<Secondhouse> getSecondhouseByPage(String houseName, int pageIndex, int pageSize) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isNotBlank(houseName)) {
            params.put("houseName", houseName);
        }
        try {
            return (List<Secondhouse>) this.getPageList(SecondhouseMapper.class, "selectByParams", params, pageIndex, pageSize);
        } catch (Exception e) {
            logger.error("分页查询楼盘失败.异常:{}", e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public int getTotal(String name) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isNotBlank(name)) {
            params.put("houseName", name);
        }
        try {
            return this.getTotal(SecondhouseMapper.class, "selectTotal", params);
        } catch (Exception e) {
            logger.error("分页查询楼盘总数失败.异常:{}", e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public List<Secondhouse> getSecondhouseByMember(Integer memberId, String houseName, int pageIndex, int pageSize) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        params.put("memberId",memberId);
        if (StringUtils.isNotBlank(houseName)) {
            params.put("houseName", houseName);
        }
        try {
            return (List<Secondhouse>) this.getPageList(SecondhouseMapper.class, "selectByMember", params, pageIndex, pageSize);
        } catch (Exception e) {
            logger.error("分页查询楼盘失败.异常:{}", e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public int getTotalByMember(Integer memberId, String houseName) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        params.put("memberId",memberId);
        if (StringUtils.isNotBlank(houseName)) {
            params.put("houseName", houseName);
        }
        try {
            return this.getTotal(SecondhouseMapper.class, "selectTotalByMember", params);
        } catch (Exception e) {
            logger.error("分页查询楼盘总数失败.异常:{}", e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }
}
